﻿#include <QtCore/QObject>
#include <Qt3DRender/qcamera.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DRender/qcameralens.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCommandLinkButton>
#include <QtGui/QScreen>
#include <Qt3DInput/QInputAspect>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include <Qt3DExtras/qorbitcameracontroller.h>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QConeMesh>
#include "qlinemesh.h"
#include "qjsonmesh.h"
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    auto *view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(0xFFFFFF));//设置视图背景色
    QWidget *widget = QWidget::createWindowContainer(view);
    const QSize screenSize = view->screen()->size();
    widget->setMinimumSize(QSize(200, 100));
    widget->setMaximumSize(screenSize);
    widget->setWindowTitle(QStringLiteral("Basic shapes"));

    //root实体
    auto *rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);
    auto *input = new Qt3DInput::QInputAspect;
    view->registerAspect(input);
    // Camera
    Qt3DRender::QCamera *cameraEntity = view->camera();
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 0, 40.0f));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));
    // 光照
    auto *lightEntity = new Qt3DCore::QEntity(rootEntity);
    auto *light       = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);
    auto *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(cameraEntity->position());
    lightEntity->addComponent(lightTransform);
    // 相机控制
    //auto *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
    auto *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    camController->setLinearSpeed(100.0);
    camController->setLookSpeed(50.0);
    camController->setCamera(cameraEntity);
    // 添加圆锥体
    auto *m_coneEntity = new Qt3DCore::QEntity(rootEntity);
    auto *cone = new Qt3DExtras::QConeMesh();
    cone->setTopRadius(0.5);
    cone->setBottomRadius(1);
    cone->setLength(3);
    cone->setRings(50);
    cone->setSlices(20);
    auto *coneTransform = new Qt3DCore::QTransform();
    coneTransform->setScale(1.5f);
    coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
    coneTransform->setTranslation(QVector3D(0.0f, 4.0f, -1.5));
    auto *coneMaterial = new Qt3DExtras::QPhongMaterial();
    coneMaterial->setDiffuse(QColor(0x928327));
    m_coneEntity->addComponent(cone);
    m_coneEntity->addComponent(coneMaterial);
    m_coneEntity->addComponent(coneTransform);

    const auto lineMaterial = new Qt3DExtras::QPhongMaterial();
    lineMaterial->setAmbient(Qt::gray);
    QVector<QVector3D> vertices;
    QVector<unsigned int> indices;
    for(int i = 0; i < 21; ++i) {
        auto fi = static_cast<float>(i-10);
        vertices.append(QVector3D{-10, fi, 0});
        vertices.append(QVector3D{10, fi, 0});
        vertices.append(QVector3D{fi, -10, 0});
        vertices.append(QVector3D{fi, 10, 0});
        indices.append(i*4+0);
        indices.append(i*4+1);
        indices.append(i*4+2);
        indices.append(i*4+3);
    }
    const auto lineEntity = new Qt3DCore::QEntity(rootEntity);
    const auto line_mesh = new QLineMesh(vertices,indices, rootEntity,Qt3DRender::QGeometryRenderer::Lines);
    lineEntity->addComponent(line_mesh);
    lineEntity->addComponent(lineMaterial);

    const auto meshMaterial = new Qt3DExtras::QPhongMaterial();
    meshMaterial->setAmbient(Qt::red);
    const auto meshEntity = new Qt3DCore::QEntity(rootEntity);
    const auto mesh_mesh = new QJsonMesh("../car_mesh.json",rootEntity);
    meshEntity->addComponent(mesh_mesh);
    meshEntity->addComponent(meshMaterial);

    widget->show();
    widget->resize(500, 500);
    return QApplication::exec();
}